<?
/**
 *会员类.
 *调用成员函数获取登录会员相关信息
 */
class Member{
    /**
     *会员ID
     *@var int
     */
    public $memberID;

    /**
     *所属分销商ID,非普通会员与会员ID一致
     *@var int
     */
    public $fid;

    /**
     *会员信息
     *@var array
     */
    public $m_info;

    /**
     *票付通内部接口对象
     *@var object
     */
    public $s;

    /**
     *构造函数.
     *@param $s 票付通内部接口对象
     *@int $memberID 会员ID
     */
    public function __construct($s,$memberID){
        if (!$memberID || !is_numeric($memberID)) {
            throw new ErrorException('用户ID为空！');
        }
        $this->fid=$this->memberID = $memberID;
//		$str="select * from pft_member where id=".$memberID." limit 1";
        $str = <<<SQL
SELECT id, dtype, account, dname, `status`, address, mobile, qq, creattime,
lasttime, headphoto, cname, ctel, endtime, smstype, smsauto, mlevel,
alipay, pay_on_order, n_send, n_accept, bank_account1, bank_account2,
fee_platform, fee_bank, fee_sms, fee_code, member_auth, fee_pay_way, inviterID
FROM pft_member WHERE id={$memberID} LIMIT 1
SQL;
        $GLOBALS['le']->query($str);
        $this->m_info=$GLOBALS['le']->fetch_assoc();
        $this->s=$s;
    }

    /**
     *会员信息函数.
     *@return array 返回会员信息键值数组
     */
    public function info(){
        return $this->m_info;
    }

    /**
     *查询常用联系人函数.
     *@param int $sort 按键值顺序|逆序排列
     *@return array 常用联系人键值数组，键联系人姓名，值联系人手机
     */
    public function cinfos($sort=1){
        $str="select cinfos from pft_member_extinfo where fid={$this->memberID} LIMIT 1";
        $GLOBALS['le']->query($str);
        $GLOBALS['le']->fetch_assoc();
        $cinfos=$GLOBALS['le']->f("cinfos");
        $out=array();
        if($cinfos){
            $cinfos=explode("|",$cinfos);
            if($sort==-1)
                krsort($cinfos);
            foreach($cinfos as $row){
                $info=explode(":",$row);
                $out[$info[0]]=$info[1];
            }
        }
        return $out;
    }

    /**
     *添加常用联系人函数.
     *@param string $ordername 联系人姓名
     *@param string $ordertel 联系人手机
     */
    public function addCinfo($ordername,$ordertel){

        if(!$ordername) return false;
        define("MAXNUM",9); //最多联系人数目
        $cinfos=$this->cinfos();
        $cname=array();
        $ctel=array();
        foreach($cinfos as $k=>$v){
            if($k){
                $cname[]=$k;
                $ctel[] =$v;
            }
        }
        $key=array_search($ordername,$cname);
        if($key===false){
            if(count($cname)>=MAXNUM){//如果数目大于做大允许数
                array_shift($cname);
                array_shift($ctel);
            }
            array_push($cname,$ordername);
            array_push($ctel,$ordertel);
        }else{
            $ctel[$key]=$ordertel;
            if($key!=(count($cname)-1)){//该联系人存在，位置移后
                $t=$cname[$key+1];
                $cname[$key+1]=$cname[$key];
                $cname[$key]=$t;
                $t=$ctel[$key+1];
                $ctel[$key+1]=$ctel[$key];
                $ctel[$key]=$t;
            }
        }

        $cinfos=array_combine($cname,$ctel);
        $out=array();

        foreach($cinfos as $k=>$v){
            $out[]=$k.":".$v;
        }
        $out=implode("|",$out);
        $str="UPDATE pft_member_extinfo set cinfos='$out' where fid={$this->memberID} LIMIT 1";
        $GLOBALS['le']->query($str);
    }

    /**
     *删除联系人信息
     *@params cutname 要删除的联系人姓名
     */
    public function cutCinfo($cutname){
        if(!$cutname) return false;
        $cinfos=$this->cinfos();
        foreach($cutname as $name){
            if(array_search($name,$cinfos)) return false;
            unset($cinfos[$name]);
        }
        foreach($cinfos as $k=>$v){
            $out[]=$k.":".$v;
        }
        $out=implode("|",$out);
        $str="update pft_member_extinfo set cinfos='$out' where  fid=".$this->memberID;
        $GLOBALS['le']->query($str);
        return "删除联系人成功";
    }
    /**
     *查询会员账户余额函数.
     *@return int 余额分单位
     */
    public function money(){
        $fundXml=$this->s->PFT_Member_Fund($this->memberID);
        $fundXml=simplexml_load_string($fundXml);
        return (string)$fundXml->Rec->UUamoney;
    }


    /**
     *查询会员账户可用授信额度函数.
     *返回值是最大可欠额度加上已有额度
     *@return int 额度分单位
     */
    public function tmoney($aid){
        $fundXml=$this->s->PFT_Member_Fund($this->memberID,2,$aid);
        $fundXml=simplexml_load_string($fundXml);
        $basefund=$fundXml->Rec->UUbasecredit;
        $fundXml=$this->s->PFT_Member_Fund($this->memberID,1,$aid);
        $fundXml=simplexml_load_string($fundXml);
        $kfund=$fundXml->Rec->UUkmoney;
        $usefund=$basefund+$kfund;//可用购买额度
        return $usefund;
    }

    /**
     *查询会员账户供应产品数函数.
     *@return int
     */
    public function pronum(){
        $str="select count(*) as num from uu_products where  p_status in (0,2,3,4,5) and apply_did=".$this->memberID;
        $GLOBALS['le']->query($str);
        $GLOBALS['le']->fetch_assoc();
        return $GLOBALS['le']->f("num");
    }

    /**
     *查询会员供应产品上架数函数.
     *@return int
     */
    public function upProNum(){
        $str="select count(*) as num from uu_products where apply_limit=1 and p_status in (0,2,3,4,5) and apply_did=".$this->memberID;
        $GLOBALS['le']->query($str);
        $GLOBALS['le']->fetch_assoc();
        return $GLOBALS['le']->f("num");
    }

    /**
     *查询会员供应产品下架数函数.
     *@return int
     */
    public function downProNum(){
        $str="select count(*) as num from uu_products where apply_limit=2 and p_status in (0,2,3,4,5) and apply_did=".$this->memberID;
        $GLOBALS['le']->query($str);
        $GLOBALS['le']->fetch_assoc();
        return $GLOBALS['le']->f("num");
    }
    public function distributorNum(){
        $str="select count(*) as num from pft_member_relationship r join pft_member m on r.son_id=m.id where r.status in (0) and m.status in (0,3) and r.ship_type=0 and parent_id=".$this->memberID." and length(m.account)=6 and son_id!={$_SESSION['sid']}";
        $GLOBALS['le']->query($str);
        $GLOBALS['le']->fetch_assoc();
        return $GLOBALS['le']->f("num");
    }

    /**
     *查询会员合作分销商函数.
     *@return array 会员ID数组
     */
    public function distributors(){
        $str = <<<SQL
select m.id from  pft_member m LEFT JOIN pft_member_relationship r on r.son_id=m.id
where r.parent_id={$this->memberID} and m.status=0 and r.ship_type=0 and r.status=0
SQL;

        $GLOBALS['le']->query($str);
        $r=array();
        while($row=$GLOBALS['le']->fetch_assoc()){
            $r[]=$row['id'];
        }
        return $r;
    }

    /**
     *查询会员合作供应商函数.
     *@return array 会员ID数组
     */
    public function appiers(){
        $str = <<<SQL
SELECT m.id FROM pft_member_relationship r LEFT JOIN pft_member m
ON r.parent_id=m.id WHERE son_id={$this->memberID} AND r.status=0
AND m.status=0 AND r.ship_type=0
SQL;
        $GLOBALS['le']->query($str);
        $r=array();
        while($row=$GLOBALS['le']->fetch_assoc()){
            $r[]=$row['id'];
        }
        return $r;
    }

    /**
     *查询已配置账单设置分销商.
     *@return array 会员ID数组
     */
    public function settlefids(){
        $str="select fid from uu_settle where aid=".$this->memberID;
        $GLOBALS['le']->query($str);
        $r=array();
        while($row=$GLOBALS['le']->fetch_assoc()){
            $r[]=$row['fid'];
        }
        return $r;
    }

    /**
     *查询已配置账单设置供应商.
     *@return array 会员ID数组
     */
    public function settleaids(){
        $str="SELECT aid FROM uu_settle WHERE fid={$this->memberID}";
        $GLOBALS['le']->query($str);
        $r=array();
        while($row=$GLOBALS['le']->fetch_assoc()){
            $r[]=$row['aid'];
        }
        return $r;
    }

    /**
     *查询会员供应产品数.
     *@return int
     */
    public function supplierNum(){
        $str="select count(*) as num from pft_member_relationship r left join pft_member m on m.id=r.parent_id where r.status=0 and r.ship_type=0 and r.son_id=".$this->memberID." and r.parent_id!={$_SESSION['sid']} and m.status in (0,3)";
        $GLOBALS['le']->query($str);
        $GLOBALS['le']->fetch_assoc();
        return $GLOBALS['le']->f("num");
    }

    /*
    *@ wx服务器 init里调用 给二级店铺选产品
    */
    public function salePros_dianpu(){
        $sql=<<<SQL
select p.id as pid,p.apply_did,p.p_status,l.id as lid,l.p_type,l.areacode,
l.title,getPY(l.title,1) as py,l.area,l.topic,l.px,l.runtime,l.taobao_support,l.imgpath,
l.address,t.id as tid,t.pay,t.getaddr,t.title as ttitle,t.tprice,t.notes,
t.buy_limit_low,t.ddays,t.delaydays,t.status,f.ass_station,
f.mdays,f.mhour,f.rdays,t.order_end,t.order_start,t.delaytype,t.sync_status
from uu_products p left join uu_land l on p.contact_id=l.id left join
uu_jq_ticket t on p.id=t.pid left join uu_land_f f on f.pid=p.id
SQL;
        $where="where p.p_status=0 and p.apply_limit=1";
        $orderby=" order by l.px desc ";
        $aids=$this->fid;
        /*查询自己供应的产品*/
        $where1=$where." AND p.apply_did in ($aids) ";
        $str1="$sql $where1 $orderby";
        $GLOBALS['le']->query($str1);
        while($row=$GLOBALS['le']->fetch_assoc()){

            $row['sapply_did']=$row['apply_did'];
            $row['aid']=0;
            $row['lvl']=0;
            $r[]=$row;
        }
        /*
        当是二级店铺来查询产品  不查询一手转分销的产品
        去掉了二手转分销里 sid!=sourceid 的条件
        --2015-3-30 zax
       */
        $str=<<<SQL
select pid,sid from pft_p_apply_evolute
WHERE fid={$this->fid} and status=0 AND active=1
SQL;
        $GLOBALS['le']->query($str);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $secondInfo[$row['pid']."_".$row['sid']]=$row;
            $secondPids[]=$row['pid'];
        }
        if(count($secondPids)!=0){
            $proInfo    = array();
            $secondPids = implode(",",$secondPids);
            $where2     = $where." and p.id in ($secondPids) ";
            $str2       = "$sql $where2 $orderby";
            $GLOBALS['le']->query($str2);
            while($row=$GLOBALS['le']->fetch_assoc()){
                $proInfo[$row['pid']]=$row;
            }
            foreach($secondInfo as $key=>$value){
                list($pid, $sapply_did) = explode("_",$key);
                if(!$proInfo[$pid]) continue;
                $row    = $proInfo[$pid];
                $row['sapply_did']=$sapply_did;
                $row['aid']=$sapply_did;
                $row['lvl']=$value['lvl'];
                $row['eid']=$value['id'];
                $row['estatus']=$value['status'];
                $aids   = explode(",",$value['aids']);
                $aids[] = $value['fid'];
                //                $count=count($aids);
                $aids[0]=0;
                $r[]=$row;
            }
        }
        return $r;
    }
    /**
     * 查询会员可分销产品信息
     *
     * @param null $lid  景区id，默认不传，传入只返回改景区产品
     * @param bool $include_evolute 发起转供应，二级域名店铺需要此参数为false
     * @param bool $include_second  是否获取转分销的产品，默认获取；取票机的目前只获取直销的产品（added at 2015年7月13日10:47:44）
     * @return array 会员可分销产品，含直接供应和转供应产品
     */
    public function salePros($lid=null, $include_evolute=true, $include_second=true)
    {

        $r=array();
///*getPY(l.title,1) as py*/,
        $sql=<<<SQL
select p.id as pid,p.apply_did,p.p_status,l.id as lid,l.p_type,l.jtype,l.areacode,
l.title,getPY(l.title,1) as py,l.salerid,l.area,l.topic,l.px,l.runtime,l.taobao_support,l.imgpath,
l.address,t.id as tid,t.pay,t.getaddr,t.title as ttitle,t.tprice,t.notes,
t.buy_limit_low,t.ddays,t.delaydays,t.overdue_refund,t.status,f.ass_station,f.tourist_info,
f.mdays,f.mhour,f.rdays,t.order_end,t.order_start,t.delaytype,t.sync_status
from uu_products p left join uu_land l on p.contact_id=l.id left join
uu_jq_ticket t on p.id=t.pid left join uu_land_f f on f.pid=p.id
SQL;
        $where="where p.p_status=0 and p.apply_limit=1";
        if($lid) $where.=" and l.id=$lid ";
        $orderby=" order by l.px desc ";
        $salePids = array();
        /*一手供应商产品*/
        //        $t1 = microtime_float();
        $aids=array(0=>$this->fid);
        $str=<<<SQL
SELECT l.pids,l.aid FROM pft_product_sale_list l
LEFT JOIN pft_member m ON l.aid=m.id
WHERE l.fid={$this->fid} AND l.status=0 AND m.status=0
SQL;
        $GLOBALS['le']->query($str);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $salePids[$row['aid']]=explode(",",$row['pids']);
            $aids[]=$row['aid'];
        }
        if(count($aids)!=0){
            $aids=implode(",",$aids);
            $where1=$where." AND p.apply_did in ($aids) ";
            $str1="$sql $where1 $orderby";
//            if ($_GET['cgp']) echo $str1;
            $GLOBALS['le']->query($str1);
            while($row=$GLOBALS['le']->fetch_assoc()){

                if($row['apply_did']!=$this->fid
                    &&$salePids[$row['apply_did']][0]!="A"
                    &&!in_array($row['pid'],$salePids[$row['apply_did']]))
                {
                    continue;
                }
                $row['sapply_did']=$row['apply_did'];
                $row['aid']=0;
                $row['lvl']=0;
                $r[]=$row;
            }
        }
        if ($include_second) {
            /*二手供应商产品*/
//        $str=<<<SQL
//select pid,sid from pft_p_apply_evolute
//WHERE fid={$this->fid} and status=0 and sid<>sourceid  and sourceid<>{$_SESSION['sid']}
//SQL;
            $str=<<<SQL
select pid,sid from pft_p_apply_evolute
WHERE fid={$this->fid} and status=0 and sid<>sourceid and sourceid<>{$this->fid}
SQL;
            if ($include_evolute===false) {
                $str .= " AND active=1";
            }
            $GLOBALS['le']->query($str);
            while($row=$GLOBALS['le']->fetch_assoc()){
                $secondInfo[$row['pid']."_".$row['sid']]=$row;
                $secondPids[]=$row['pid'];
            }
//        if($_GET['cs']){
//            echo $str;
//            print_r($secondPids);
//            exit;
//        }
            if(count($secondPids)!=0){
                $proInfo    = array();
                $secondPids = implode(",",$secondPids);
                $where2     = $where." and p.id in ($secondPids) ";
                $str2       = "$sql $where2 $orderby";

                $GLOBALS['le']->query($str2);
                while($row=$GLOBALS['le']->fetch_assoc()){
                    $proInfo[$row['pid']]=$row;
                }
                foreach($secondInfo as $key=>$value){
                    list($pid, $sapply_did) = explode("_",$key);
                    if(!$proInfo[$pid]) continue;
                    $row    = $proInfo[$pid];
                    $row['sapply_did']=$sapply_did;
                    $row['aid']=$sapply_did;
                    $row['lvl']=$value['lvl'];
                    $row['eid']=$value['id'];
                    $row['estatus']=$value['status'];
                    $aids   = explode(",",$value['aids']);
                    $aids[] = $value['fid'];
//                $count=count($aids);
                    $aids[0]=0;
                    $r[]=$row;
                }
            }
        }

        return $r;
    }

    /**
     *查询会员可分销产品信息.
     *@param $lid 景区id，默认不传，传入只返回改景区产品
     *@return array 会员可分销产品，含直接供应和转供应产品
     */
    public function salePros_n($lid=Null){

        if($_SESSION['saccount']){
            $account = $_SESSION['saccount']; // 会员账号
        }else{
            $account = $_SESSION['account'];
        }
        $r=array();
        $salePids=array();
        $str="select * from uu_priceset where pid=".$this->fid;
        $GLOBALS['le']->query($str);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $dprices[$row['tid']."_".$row['aid']]=$row['dprice'];
        }
        $sql="select p.id as pid,p.apply_did,p.p_status,l.id as lid,l.p_type,"
            ."l.title,getPY(l.title,1) as py,l.area,l.topic,l.px,l.runtime,l.taobao_support,l.imgpath,"
            ."l.address,t.id as tid,t.pay,t.getaddr,t.title as ttitle,t.tprice,t.notes,"
            ."t.buy_limit_low,t.ddays,t.delaydays,t.status,f.ass_station,"
            ."f.mdays,f.mhour,f.rdays,t.order_end,t.delaytype,t.sync_status from uu_products p left join uu_land l on p.contact_id=l.id left join
			uu_jq_ticket t on p.id=t.pid left join uu_land_f f on f.pid=p.id";
        $where="where p.p_status in (0,3,4,5) and p.apply_limit=1";
        if($lid) $where.=" and l.id=$lid ";
        $orderby=" order by l.px desc ";

        /*一手供应商产品*/
        $aids=array(0=>$this->fid);
        $str="select l.pids,l.aid from pft_product_sale_list l join pft_member m on l.aid=m.id where l.fid=".$this->fid." and l.status=0 and m.status=0";
        $GLOBALS['le']->query($str);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $salePids[$row['aid']]=explode(",",$row['pids']);
            $aids[]=$row['aid'];
        }
        if(count($aids)!=0){
            $aids=implode(",",$aids);
            $where1=$where." and p.apply_did in ($aids) ";
            $str1="$sql $where1 $orderby";

            $GLOBALS['le']->query($str1);
            while($row=$GLOBALS['le']->fetch_assoc()){
                // print_r($row);
                if($row['apply_did']!=$this->fid&&$salePids[$row['apply_did']][0]!="A"&&!in_array($row['pid'],$salePids[$row['apply_did']])) continue;

                // 从接口直接获取结算价
                // echo "Dynamic_Price_And_Storage($account,{$row['pid']},,1,1,0,{$row['apply_did']}<br/>";
                $xml = $this->s->Dynamic_Price_And_Storage($account,$row['pid'],date("Y-m-d"),1,0,0,$row['apply_did']);
                if($xml==105){
                    $row['jsprice']=-1;
                }else{
                    $object = simplexml_load_string($xml);
                    $row['jsprice']=(string)$object->Rec->UUsprice;
                }


                // 从接口获取零售价
                // echo "Dynamic_Price_And_Storage($account,{$row['pid']},,1,1,0,{$row['apply_did']}";
                $xml = $this->s->Dynamic_Price_And_Storage($account,$row['pid'],date("Y-m-d"),1,1,0,$row['apply_did']);
                if($xml==105){
                    $row['uprice']=-1;
                }else{
                    $object = simplexml_load_string($xml);
                    $row['uprice']=(string)$object->Rec->UUsprice;
                }


                if($row['jsprice']==-1||$row['uprice']==-1) continue;
                // $row['jsprice']=$row['jsprice']+$dprices[$row['pid']."_0"];
                $row['sapply_did']=$row['apply_did'];
                $row['aid']=0;
                $row['lvl']=0;
                $r[]=$row;
            }
        }

        /*二手供应商产品*/
        $str="select * from pft_p_apply_evolute where fid=".$this->fid." and status=0 and sid<>sourceid";
        $GLOBALS['le']->query($str);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $secondInfo[$row['pid']."_".$row['sid']]=$row;
            $secondPids[]=$row['pid'];
        }
        if(count($secondPids)!=0){
            $dprices2=array();
            $str="select * from uu_priceset where tid in (".implode(",",$secondPids).")";
            $GLOBALS['le']->query($str);
            while($row=$GLOBALS['le']->fetch_assoc()){
                $dprices2[$row['tid']."_".$row['pid']."_".$row['aid']]=$row['dprice'];
            }
            $proInfo=array();
            $secondPids=implode(",",$secondPids);
            $where2=$where." and p.id in ($secondPids) ";
            $str2="$sql $where2 $orderby";
            $GLOBALS['le']->query($str2);
            while($row=$GLOBALS['le']->fetch_assoc()){
                $proInfo[$row['pid']]=$row;
            }
            foreach($secondInfo as $key=>$value){
                $key=explode("_",$key);
                $pid=$key[0];
                $sapply_did=$key[1];
                if(!$proInfo[$pid]) continue;

                // 从接口直接获取结算价
                // echo "Dynamic_Price_And_Storage($account,$pid,,1,0,0,$sapply_did)<br/>";
                $xml = $this->s->Dynamic_Price_And_Storage($account,$pid,date("Y-m-d"),1,0,0,$sapply_did);


                if($xml==105){
                    $proInfo[$pid]['jsprice']=-1;
                }else{
                    $object = simplexml_load_string($xml);
                    $proInfo[$pid]['jsprice']=(string)$object->Rec->UUsprice;
                }
                // 从接口获取零售价
                $xml = $this->s->Dynamic_Price_And_Storage($account,$pid,date("Y-m-d"),1,1,0,$apply_did);
                if($xml==105){
                    $proInfo[$pid]['uprice']=-1;
                }else{
                    $object = simplexml_load_string($xml);
                    $proInfo[$pid]['uprice']=(string)$object->Rec->UUsprice;
                }

                if($proInfo[$pid]['jsprice']==-1||$proInfo[$pid]['uprice']==-1) continue;
                $row=$proInfo[$pid];
                $row['sapply_did']=$sapply_did;
                $row['aid']=$sapply_did;
                $row['lvl']=$value['lvl'];
                $row['eid']=$value['id'];
                $row['estatus']=$value['status'];
                $aids=explode(",",$value['aids']);
                $aids[]=$value['fid'];
                $count=count($aids);
                $aids[0]=0;
                // for($i=1;$i<$count;$i++){
                // $row['jsprice']+=$dprices2[$row['pid']."_".$aids[$i]."_".$aids[$i-1]];
                // }
                $r[]=$row;
            }
        }
        // exit;
        return $r;
    }

    /**
     *查询会员可分销产品信息.
     *@param $lid 景区id，默认不传，传入只返回改景区产品
     *@return array 会员可分销产品，含直接供应和转供应产品
     *  产品下架也显示出来，销售报表调用
     */
    public function _salePros($lid=Null){
        $r=array();
        $salePids=array();
        $str="select * from uu_priceset where pid=".$this->fid;
        $GLOBALS['le']->query($str);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $dprices[$row['tid']."_".$row['aid']]=$row['dprice'];
        }
        $sql="select p.id as pid,p.apply_did,p.p_status,l.id as lid,l.p_type,"
            ."l.title,l.area,l.topic,l.px,l.runtime,l.taobao_support,l.imgpath,"
            ."l.address,t.id as tid,t.pay,t.getaddr,t.title as ttitle,t.tprice,t.notes,"
            ."t.buy_limit_low,t.ddays,t.delaydays,t.status,f.ass_station,"
            ."f.mdays,f.mhour,f.rdays from uu_products p left join uu_land l on p.contact_id=l.id left join
			uu_jq_ticket t on p.id=t.pid left join uu_land_f f on f.pid=p.id";
        $where="where p.p_status in (0,3,4,5) and p.apply_limit=1";
        if($lid) $where.=" and l.id=$lid ";
        $orderby=" order by l.px desc ";

        /*一手供应商产品*/
        $aids=array(0=>$this->fid);
        $str="select l.pids,l.aid from pft_product_sale_list l join pft_member m on l.aid=m.id where l.fid=".$this->fid." and m.status=0";
        $GLOBALS['le']->query($str);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $salePids[$row['aid']]=explode(",",$row['pids']);
            $aids[]=$row['aid'];
        }
        if(count($aids)!=0){
            $aids=implode(",",$aids);
            $where1=$where." and p.apply_did in ($aids) ";
            $str1="$sql $where1 $orderby";
            $GLOBALS['le']->query($str1);
            while($row=$GLOBALS['le']->fetch_assoc()){
                if($row['apply_did']!=$this->fid&&$salePids[$row['apply_did']][0]!="A"&&!in_array($row['pid'],$salePids[$row['apply_did']])) continue;

                if(($row['jsprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1))==-1){
                    $row['jsprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2);
                }
                if(($row['uprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1,"","",1))==-1){
                    $row['uprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2,"","",1);
                }
                if($row['jsprice']==-1||$row['uprice']==-1) continue;
                $row['jsprice']=$row['jsprice']+$dprices[$row['pid']."_0"];

                $row['sapply_did']=$row['apply_did'];
                $row['aid']=0;
                $row['lvl']=0;
                $r[]=$row;
            }
        }

        /*二手供应商产品*/
        $str="select * from pft_p_apply_evolute where fid=".$this->fid." and status=0 and sid<>sourceid";
        $GLOBALS['le']->query($str);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $secondInfo[$row['pid']."_".$row['sid']]=$row;
            $secondPids[]=$row['pid'];
        }
        if(count($secondPids)!=0){
            $dprices2=array();
            $str="select * from uu_priceset where tid in (".implode(",",$secondPids).")";
            $GLOBALS['le']->query($str);
            while($row=$GLOBALS['le']->fetch_assoc()){
                $dprices2[$row['tid']."_".$row['pid']."_".$row['aid']]=$row['dprice'];
            }
            $proInfo=array();
            $secondPids=implode(",",$secondPids);
            $where2=$where." and p.id in ($secondPids) ";
            $str2="$sql $where2 $orderby";
            $GLOBALS['le']->query($str2);
            while($row=$GLOBALS['le']->fetch_assoc()){
                if(($row['jsprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1))==-1){
                    $row['jsprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2);
                }
                if(($row['uprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1,"","",1))==-1){
                    $row['uprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2,"","",1);
                }
                if($row['jsprice']==-1||$row['uprice']==-1) continue;
                $proInfo[$row['pid']]=$row;
            }
            foreach($secondInfo as $key=>$value){
                $key=explode("_",$key);
                $pid=$key[0];
                $sapply_did=$key[1];
                if(!$proInfo[$pid]) continue;
                $row=$proInfo[$pid];
                $row['sapply_did']=$sapply_did;
                $row['aid']=$sapply_did;
                $row['lvl']=$value['lvl'];
                $row['eid']=$value['id'];
                $row['estatus']=$value['status'];

                $aids=explode(",",$value['aids']);

                $aids[]=$value['fid'];
                $count=count($aids);
                $aids[0]=0;
                for($i=1;$i<$count;$i++){
                    $row['jsprice']+=$dprices2[$row['pid']."_".$aids[$i]."_".$aids[$i-1]];
                }
                $r[]=$row;
            }
        }
        return $r;
    }

    /**
     *查询会员可分销产品信息.   传入景区ID 多个
     *@param $lid 景区id，默认不传，传入只返回改景区产品
     *@return array 会员可分销产品，含直接供应和转供应产品
     */
    public function salePros_fuzzy($lid=Null){
        $r=array();
        $salePids=array();
        $str="select * from uu_priceset where pid=".$this->fid;
        $GLOBALS['le']->query($str);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $dprices[$row['tid']."_".$row['aid']]=$row['dprice'];
        }
        $sql="select p.id as pid,p.apply_did,p.p_status,l.id as lid,l.p_type,"
            ."l.title,l.area,l.topic,l.px,l.runtime,l.taobao_support,l.imgpath,"
            ."l.address,t.id as tid,t.pay,t.getaddr,t.title as ttitle,t.tprice,t.notes,"
            ."t.buy_limit_low,t.ddays,t.delaydays,t.status,f.ass_station,"
            ."f.mdays,f.mhour,f.rdays from uu_products p left join uu_land l on p.contact_id=l.id left join
			uu_jq_ticket t on p.id=t.pid left join uu_land_f f on f.pid=p.id";
        $where="where p.p_status in (0,3,4,5) and p.apply_limit=1";
        if($lid) $where.=" and l.id IN ($lid)";
        $orderby=" order by l.px desc ";

        /*一手供应商产品*/
        $aids=array(0=>$this->fid);
        $str="select l.pids,l.aid from pft_product_sale_list l join pft_member m on l.aid=m.id where l.fid=".$this->fid." and l.status=0 and m.status=0";
        $GLOBALS['le']->query($str);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $salePids[$row['aid']]=explode(",",$row['pids']);
            $aids[]=$row['aid'];
        }
        if(count($aids)!=0){
            $aids=implode(",",$aids);
            $where1=$where." and p.apply_did in ($aids) ";
            $str1="$sql $where1 $orderby";
            $GLOBALS['le']->query($str1);
            while($row=$GLOBALS['le']->fetch_assoc()){
                if($row['apply_did']!=$this->fid&&$salePids[$row['apply_did']][0]!="A"&&!in_array($row['pid'],$salePids[$row['apply_did']])) continue;

                if(($row['jsprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1))==-1){
                    $row['jsprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2);
                }
                if(($row['uprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1,"","",1))==-1){
                    $row['uprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2,"","",1);
                }
                if($row['jsprice']==-1||$row['uprice']==-1) continue;
                $row['jsprice']=$row['jsprice']+$dprices[$row['pid']."_0"];

                $row['sapply_did']=$row['apply_did'];
                $row['aid']=0;
                $row['lvl']=0;
                $r[]=$row;
            }
        }

        /*二手供应商产品*/
        $str="select * from pft_p_apply_evolute where fid=".$this->fid." and status=0 and sid<>sourceid";
        $GLOBALS['le']->query($str);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $secondInfo[$row['pid']."_".$row['sid']]=$row;
            $secondPids[]=$row['pid'];
        }
        if(count($secondPids)!=0){
            $dprices2=array();
            $str="select * from uu_priceset where tid in (".implode(",",$secondPids).")";
            $GLOBALS['le']->query($str);
            while($row=$GLOBALS['le']->fetch_assoc()){
                $dprices2[$row['tid']."_".$row['pid']."_".$row['aid']]=$row['dprice'];
            }
            $proInfo=array();
            $secondPids=implode(",",$secondPids);
            $where2=$where." and p.id in ($secondPids) ";
            $str2="$sql $where2 $orderby";
            $GLOBALS['le']->query($str2);
            while($row=$GLOBALS['le']->fetch_assoc()){
                if(($row['jsprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1))==-1){
                    $row['jsprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2);
                }
                if(($row['uprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),1,"","",1))==-1){
                    $row['uprice']=$this->s->get_Dynamic_Price_Merge($row['pid'],date("Y-m-d"),2,"","",1);
                }
                if($row['jsprice']==-1||$row['uprice']==-1) continue;
                $proInfo[$row['pid']]=$row;
            }
            foreach($secondInfo as $key=>$value){
                $key=explode("_",$key);
                $pid=$key[0];
                $sapply_did=$key[1];
                if(!$proInfo[$pid]) continue;
                $row=$proInfo[$pid];
                $row['sapply_did']=$sapply_did;
                $row['aid']=$sapply_did;
                $row['lvl']=$value['lvl'];
                $row['eid']=$value['id'];
                $row['estatus']=$value['status'];

                $aids=explode(",",$value['aids']);

                $aids[]=$value['fid'];
                $count=count($aids);
                $aids[0]=0;
                for($i=1;$i<$count;$i++){
                    $row['jsprice']+=$dprices2[$row['pid']."_".$aids[$i]."_".$aids[$i-1]];
                }
                $r[]=$row;
            }
        }
        return $r;
    }

}

/**
 *普通会员类.
 *可分销产品以传入分销商ID为准，分销价按零售价计算
 */
class GuestMember extends Member{
    public function __construct($s,$memberID,$fid){
        parent::__construct($s,$memberID);
        $this->fid=$fid;
    }
}
?>